package hjw.com.config;

import hjw.com.dao.UserMapper;
import hjw.com.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
    /**
     * <p>Project:Academic - UserDetailsServiceImpl
     *
     * @author IvanWei玮 -- maste
     * @version 1.0
     * @DataTime 2025/1/20 14:13
     * @since 1.8
     */
@Component
public class UserDetailsServiceImpl
        implements UserDetailsService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final UserMapper userDao;

    @Autowired
    public UserDetailsServiceImpl(UserMapper userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //user 浏览器登录窗口输入的账号 《input v-model="username" <td><input type="text" name="username"/></td>
        User use = userDao.findOneByAccount(username);
        logger.debug(""+use);
        logger.debug("loadUserByUsername-数据库密码:" +  use.getPassword());
        //提供 UserDetails 的 实现类 User

        GrantedAuthority p1 =
                new SimpleGrantedAuthority(use.getRole());
        // 权限1

        List< GrantedAuthority> lst = new ArrayList<>();
        lst.add(p1);

        // org.springframework.security.core.userdetails.User;
        // 向上转型
        return new org.springframework.security.core.userdetails.User(username, use.getPassword(), lst);
    }
}

//System.out.println("怎样比对加密结果是由 111 加密产生");
// System.out.println(p2.matches("111", s1));